﻿using System;
using System.Net;

using mvcForum.Core.Interfaces.Events;

namespace mvcForum.Web.Events {

	public class InstallationEventListener : IAsyncEventListener<InstallationEvent> {
		private readonly IAsyncTask task;

		public InstallationEventListener(IAsyncTask task) {
			this.task = task;
		}

		public void Queue(InstallationEvent payload) {
			this.task.Execute(this, payload, 2);
		}

		public void Handle(InstallationEvent payload) {
			new WebClient().DownloadData(String.Format("http://mvcforum.org/api/json/installation/add?version={0}", payload.Version));
		}

		public void Handle(Object payload) {
			if (payload is InstallationEvent) {
				this.Handle((InstallationEvent)payload);
			}
			else {
				throw new ApplicationException("Unknown payload!");
			}
		}

		public Boolean RunAsynchronously {
			get {
				return false;
			}
		}

		public Byte Priority {
			get {
				return (Byte)EventPriority.Medium;
			}
		}

		public Boolean UniqueEvent {
			get {
				return true;
			}
		}
	}
}